package com.yiren.backstage.communication.utils;

import org.apache.commons.lang.StringUtils;

/**
 * Created by WANGRONGXIANG
 * on 2022/3/3
 * Describe 数组转换的工具类
 */
public class ArrayConvertUtil {

    /**
     * 将浮点型二维数组转换为String
     *
     * @param arrs
     * @return
     */
    public static String convertFloats2String(float[][] arrs) {
        String s = "";
        for (int i = 0; i < arrs.length; i++) {
            float[] arr = arrs[i];
            for (int j = 0; j < arr.length; j++) {
                s += arr[j] + ",";
            }
        }
        return s.substring(0, s.length() - 1);
    }

    /**
     * 转换字符串为二维浮点型数组
     *
     * @param data
     * @param row
     * @param col
     * @return
     */
    public static float[][] convertString2Floats(String data, int row, int col) {
        float[][] arrs = new float[row][col];
        int count = 0;
        String[] strArray = data.split(",");
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                arrs[i][j] = Float.parseFloat(StringUtils.isNotEmpty(strArray[count]) ? strArray[count] : "0");
                ++count;
            }
        }
        return arrs;
    }

}
